const app = getApp()
const dayjs = app.dayjs
const login_js = require('../../utils/login.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_dark: false,
    triggered: false,
    list: [],
    modal_show: false,
    type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      is_datk:app.global_data.darkness
    })
    let type = options.type
    this.setData({
      type
    })
    let title
    switch (type) {
      case 'twenty_four_math':
        title = '算二十四点排行榜TOP20'
        break
      case 'sudoku':
        title = '玩转数独排行榜TOP20'
        break
      case 'klotski':
        title = '数字华容道排行榜TOP20'
        break
      default:
        title = '排行榜'
    }
    wx.setNavigationBarTitle({
      title: title,
    })
    this.getList(type)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  ...login_js.methods,
  /**
   * 登录时候获取用户信息回调
   */
  async getUserInfo(e) {
    await this.getProfile()
    this.clickModalCancel()
  },
  clickModalCancel() {
    this.setData({
      is_user: true,
      modal_show: false,
    })
  },
  getList(type) {
    wx.cloud.callFunction({
      name: 'getMiniGameRank',
      data: {
        collection: type,
        skip: 0,
        limit: 20
      },
      success: res => {
        // console.log(res)
        let list = res.result.list
        if (!list.length) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none'
          })
          return
        }
        list.forEach(i => {
          i.user_info = i.user_data[0].user_info
        })
        // console.log(list)
        this.setData({
          list: [...this.data.list, ...list]
        })
      },
      fail: res => {
        wx.hideLoading({})
        wx.showToast({
          title: '获取排名失败',
          icon: 'none'
        })
        console.log(res.errMsg)
      }
    })
  },
  clickGame() {
    // 登录
    // 如果未登录则弹框
    if (!app.global_data.user_data.openid) {
      this.setData({
        modal_show: true
      })
      return
    }
    let url
    switch (this.data.type) {
      case 'twenty_four_math':
        url = '/pages/twenty_four_math/twenty_four_math'
        break
      case 'sudoku':
        url = '/pages/sudoku/sudoku'
        break
      case 'klotski':
        url = '/pages/klotski/klotski'
        break
      default:
        break
    }
    wx.reLaunch({
      url: url,
    })
  }
})